package com.jia.leetCode;

import java.util.PriorityQueue;

public class Pro0378 {
    //暴力
    public int kthSmallest(int[][] matrix, int k) {
        int rows = matrix.length, cols = matrix[0].length;
        if(k == 0 || rows * cols < k) { return 0;}
        PriorityQueue<Integer> head = new PriorityQueue<>((o1, o2)->o2-o1);
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                // if(head.size() == k && matrix[i][j] > head.peek()) break;
                head.add(matrix[i][j]);
                if(head.size() > k) {
                    head.poll();
                }
            }
        }
        return head.peek();
    }
}
